package com.xsq.rpc.center.server.config;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.xsq.rpc.common.config.ZookeeperConfigurationCentreProperties;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * ClassName: DruidDataSourceHandler <br/>
 * Description: <br/>
 * date: 2020-05-20 17:19<br/>
 *
 * @author colde<br />
 */
@Component
public class DruidDataSourceHandler {


    @Getter
    private DataSource dataSource;

    @Autowired
    private ZookeeperConfigurationCentreProperties properties;


    public DataSource updateDataSource(ZookeeperConfigurationCentreProperties properties) throws Exception {
        this.properties = properties;
        Map<String, String> dataSourceConfig = new HashMap();
        dataSourceConfig.put(DruidDataSourceFactory.PROP_DRIVERCLASSNAME, properties.getDriverClassName());
        dataSourceConfig.put(DruidDataSourceFactory.PROP_URL, properties.getJdbcUrl());
        dataSourceConfig.put(DruidDataSourceFactory.PROP_USERNAME, properties.getUsername());
        dataSourceConfig.put(DruidDataSourceFactory.PROP_PASSWORD, properties.getPassword());
        DataSource dataSource = DruidDataSourceFactory.createDataSource(dataSourceConfig);
        System.out.println("新节点数据为：" + dataSourceConfig);
        this.dataSource = dataSource;
        return dataSource;
    }

}
